package com.dff.jrfw.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.dff.jrfw.R;

public class LoadingDialog extends Dialog {

    private ImageView p1;
    private ImageView p2;
    private ImageView p3;
    private Animation a1;
    private Animation a2;
    private Animation a3;

    public LoadingDialog(@NonNull Context context) {
        super(context, R.style.TranslucentDialog);
        setCanceledOnTouchOutside(false);   // 点击其他地方dialog不消失
        setCancelable(false);         // 点击返回键dialog不消失
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loding_dialog);
        p1 = findViewById(R.id.p1);
        p2 = findViewById(R.id.p2);
        p3 = findViewById(R.id.p3);
        a1 = new ScaleAnimation(0.5f, 1, 0.5f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        a2 = new ScaleAnimation(0.5f, 1, 0.5f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        a3 = new ScaleAnimation(0.5f, 1, 0.5f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        a1.setRepeatMode(Animation.REVERSE);
        a2.setRepeatMode(Animation.REVERSE);
        a3.setRepeatMode(Animation.REVERSE);
        a1.setDuration(450);
        a2.setDuration(450);
        a3.setDuration(450);
        a1.setRepeatCount(-1);
        a2.setRepeatCount(-1);
        a3.setRepeatCount(-1);
    }

    @Override
    public void show() {
        super.show();
        p1.startAnimation(a1);
        p2.postDelayed(() -> p2.startAnimation(a2),150);
        p3.postDelayed(() -> p3.startAnimation(a3),300);
    }

    @Override
    public void cancel() {
        super.cancel();
        p1.clearAnimation();
        p2.clearAnimation();
        p3.clearAnimation();
    }
}
